package cn.onesdream.app.api.service.Impl;

import cn.onesdream.app.api.service.ChatService;
import cn.onesdream.app.api.websocket.AppWebSocketEndpoint;
import cn.onesdream.app.dao.entity.Chat;
import cn.onesdream.app.dao.entity.User;
import cn.onesdream.app.dao.mapper.ChatMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

@Service
public class ChatServiceImpl extends ServiceImpl<ChatMapper, Chat> implements ChatService {
    @Autowired
    private ChatMapper chatMapper;

    @Autowired
    private AppWebSocketEndpoint appWebSocketEndpoint;

    @Override
    public Page<Chat> listHistoryMsg(Integer toUserId, Integer fromUserId, String beginDate, String endDate, Integer size, Integer current) {
        if ("".equals(beginDate) || "".equals(endDate)){
            beginDate = null;
            endDate = null;
        }
        if (size == null){
            size = 10;
        }
        QueryWrapper<Chat> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .select(Chat::getFromUserId, Chat::getId, Chat::getContent, Chat::getUpdateTime)
                .eq(Chat::getToUserId, toUserId);
        if (fromUserId != null){
            wrapper.lambda().eq(Chat::getFromUserId, fromUserId);
        }
        if (beginDate != null && endDate != null){
            wrapper.lambda().between(Chat::getUpdateTime, beginDate, endDate);
        }
        Page<Chat> page = new Page<>(current, size);
        Page<Chat> chatPage = chatMapper.selectPage(page, wrapper);
        return chatPage;
    }

    @Override
    public Page<Chat> listUnreadMsg(Integer toUserId, Integer current, Integer size) {
        if (current == null || size == null){
            current = 1;
            size = 1000;
        }
        QueryWrapper<Chat> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(Chat::getToUserId, toUserId)
                .eq(Chat::getIsRead, (byte) 0)
                .orderByDesc(Chat::getFromUserId, Chat::getUpdateTime);
        Page<Chat> page = new Page<>(current, size);
        Page<Chat> pageList = chatMapper.selectPage(page, wrapper);
        return pageList;
    }


    @Override
    public void readMsg(Integer msgId) {
        UpdateWrapper<Chat> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda()
                .set(Chat::getIsRead, (byte) 1)
                .eq(Chat::getId, msgId)
                .eq(Chat::getIsRead, (byte) 0);
        chatMapper.update(null, updateWrapper);
    }

    @Override
    public void isReadUserMsg(Integer fromUserId) {
        UpdateWrapper<Chat> updateWrapper = new UpdateWrapper<>();
        Integer toUserId = ((User) SecurityContextHolder.getContextHolderStrategy().getContext().getAuthentication().getPrincipal()).getId();
        updateWrapper.lambda()
                .set(Chat::getIsRead, (byte) 1)
                .eq(Chat::getToUserId, toUserId)
                .eq(Chat::getFromUserId, fromUserId)
                .eq(Chat::getIsRead, (byte) 0);
        chatMapper.update(null, updateWrapper);
    }
}
